/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package org.voltor.core.nodes;

import java.beans.IntrospectionException;
import java.util.ArrayList;
import java.util.List;
import org.openide.ErrorManager;
import org.openide.nodes.*;
import org.openide.util.Exceptions;
import org.openide.util.lookup.Lookups;
import org.voltor.core.Device;

/**
 *
 * @author alext
 */
public class DeviceNodeCreationFactory extends ChildFactory<Device> {

    private List<Device> resultList;

    public DeviceNodeCreationFactory() {
        this.resultList = new ArrayList<Device>();
    }

    public DeviceNodeCreationFactory(Device device) {
        this();
        List<Device> t = new ArrayList<Device>();
        t.add(device);
    }

    public DeviceNodeCreationFactory(List<Device> resultList) {
        this.resultList = resultList;
    }

    @Override
    protected boolean createKeys(List<Device> list) {
        for (Device device : resultList) {
            list.add(device);
        }
        return true;
    }

    @Override
    protected Node createNodeForKey(Device key) {
        try {
            return new DeviceNode(key);
        } catch (IntrospectionException ex) {
            ex.printStackTrace();
            Exceptions.printStackTrace(ex);
        }
        return null;
    }

    private class DeviceNode extends AbstractNode {

        public DeviceNode(Device obj) throws IntrospectionException {
            // super(obj);
            super(Children.create(new DeviceNodeCreationFactory(obj), true), Lookups.singleton(obj));
            setDisplayName(obj.getName());
        }

        @Override
        protected Sheet createSheet() {
            Sheet sheet = Sheet.createDefault();
            Sheet.Set set = Sheet.createPropertiesSet();
            Device obj = getLookup().lookup(Device.class);
            try {
                Node.Property typeProp = new PropertySupport.Reflection(obj, String.class, "getType", null);
                Node.Property nameProp = new PropertySupport.Reflection(obj, String.class, "getName", null);
                Node.Property descriptionProp = new PropertySupport.Reflection(obj, String.class, "getDescription", null);

                typeProp.setName("Type");
                nameProp.setName("Name");
                descriptionProp.setName("Description");

                set.put(typeProp);
                set.put(nameProp);
                set.put(descriptionProp);

            } catch (NoSuchMethodException ex) {
                ErrorManager.getDefault();
            }

            sheet.put(set);
            return sheet;

        }

        @Override
        public String getHtmlDisplayName() {
            Device obj = getLookup().lookup(Device.class);
            if (obj != null) {
                return "<font color='!textText'> " + obj.getName() + "</font>"
                        + "<font color='!controlShadow'><i>" + obj.getType() + "</i></font>";
            } else {
                return null;
            }
        }
    }
}
